package com.example.demo.controller;


import com.example.demo.common.BaseResponse;
import com.example.demo.common.TokenUtils;
import com.example.demo.entity.Employee;
import com.example.demo.entity.dto.QueryVacation;
import com.example.demo.entity.dto.VacationDto;
import com.example.demo.service.TbVacationService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author qjt
 * @since 2022-10-09
 */
@RestController
@RequestMapping("/tb-vacation")
public class TbVacationController {

    Logger logger = LoggerFactory.getLogger(EmployeeController.class);

    @Autowired
    private TbVacationService tbVacationService;

    @Autowired
    TokenUtils tokenUtils;

    @PostMapping("/queryAll")
    public BaseResponse queryVacation(@RequestBody QueryVacation queryVacation, HttpServletRequest request) {
        logger.info("queryVacation");

        //查询当前用户
        Employee currentEmp = tokenUtils.getCurrentEmp(request);
        /**
         * 查询当前用户，如果role_id 那么只能查看自己的假条
         * 如果不等于1，那么可以查看所有的假条
         * 经理只能看到请假单的状态是“等待经理审批”这样的请假单，CEO只能看到状态是“等待CEO审批”的请假单
         * .超级管理员admin登录后，可以看到所有的请假单
         */
        if (currentEmp.getRoleId() != null) {

            queryVacation.setId(currentEmp.getRoleId());
            queryVacation.setCurrentPage(1);
            queryVacation.setPageSize(10);
            PageInfo<VacationDto> queryDishVoPageInfo = tbVacationService.queryByPageAndCondition(queryVacation,currentEmp.getRoleId());
            return BaseResponse.success().data("pageInfo", queryDishVoPageInfo);
        }

        return BaseResponse.fail().message("请从新登录");
    }

}

